package com.fireline.beauty.seting;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataCombo;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;

/**
 * 服务项目管理界面
 * */
public class FfxmView implements IFunction {
	private Text text;
	/**
	 * @wbp.nonvisual location=17,372
	 */
	private final Dataset fwxmSet = new Dataset(true, "fwxm",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=87,372
	 */
	private final Dataset lbSet = new Dataset("select * from fwlb",
			SqliteJdbcImpl.INSTANCE);
	private DataCombo dataCombo;
	private Button btnCheckButton;

	private Control content;
	/**
	 * @wbp.nonvisual location=13,432
	 */
	private final Batch delFwxm = new Batch(SqliteJdbcImpl.INSTANCE);
	private DataTable dataTable;

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		fwxmSet.setKeyColumns(new String[] { "xmbh" });
		lbSet.setKeyColumns(new String[] { "id" });

		BatchItem batchItem = new BatchItem(delFwxm);
		batchItem.setCommand("delete from fwxm where xmbh=?");
		batchItem.setName("删除服务项目");
		Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		ToolBar toolBar = new ToolBar(content, SWT.FLAT | SWT.RIGHT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem.setText("增加");
		tltmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doAdd();
			}
		});
		ToolItem tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doEdit();
			}
		});
		tltmNewItem_1.setText("修改");

		ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_2.setText("删除");
		tltmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doRemove();
			}
		});

		ToolItem tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_3.setText("打印");

		ToolItem tltmNewItem_4 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_4.setText("导入");

		ToolItem tltmNewItem_5 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_5.setText("导出");

		ToolItem tltmNewItem_6 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_6.setText("退出");

		Label label = new Label(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));

		Composite composite = new Composite(content, SWT.NONE);
		composite.setLayout(new GridLayout(6, false));
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel.setText("项目类别：");

		dataCombo = new DataCombo(composite, SWT.NONE, lbSet);
		dataCombo.setShowText("${lbmc}");
		dataCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));

		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblNewLabel_1.setText("项目编号或名称：");

		text = new Text(composite, SWT.BORDER);
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_text.widthHint = 150;
		text.setLayoutData(gd_text);

		Button btnNewButton = new Button(composite, SWT.NONE);
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = 60;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSearch();
			}
		});
		btnNewButton.setText("查询");

		btnCheckButton = new Button(composite, SWT.CHECK);
		btnCheckButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSearch();
			}
		});
		btnCheckButton.setText("显示禁用项目");

		dataTable = new DataTable(content, SWT.BORDER | SWT.FULL_SELECTION,
				fwxmSet);
		dataTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1,
				1));
		dataTable.setHeaderVisible(true);
		dataTable.setLinesVisible(true);

		DataTableColumn dataTableColumn = new DataTableColumn(dataTable,
				SWT.NONE, "xmbh");
		dataTableColumn.setText("编号");
		dataTableColumn.setWidth(80);

		DataTableColumn dataTableColumn_1 = new DataTableColumn(dataTable,
				SWT.NONE, "xmmc");
		dataTableColumn_1.setText("项目名称");
		dataTableColumn_1.setWidth(80);

		DataTableColumn dataTableColumn_2 = new DataTableColumn(dataTable,
				SWT.NONE, "lb");
		dataTableColumn_2.setText("所属类别");
		dataTableColumn_2.setWidth(80);

		DataTableColumn dataTableColumn_3 = new DataTableColumn(dataTable,
				SWT.NONE, "dj");
		dataTableColumn_3.setText("单价");
		dataTableColumn_3.setWidth(80);

		DataTableColumn dataTableColumn_4 = new DataTableColumn(dataTable,
				SWT.RIGHT, "fwsj");
		dataTableColumn_4.setText("服务时间");
		dataTableColumn_4.setWidth(120);

		DataTableColumn dataTableColumn_5 = new DataTableColumn(dataTable,
				SWT.NONE, "jy");
		dataTableColumn_5.setText("禁用");
		dataTableColumn_5.setWidth(80);

		DataTableColumn dataTableColumn_6 = new DataTableColumn(dataTable,
				SWT.NONE, "hfzq");
		dataTableColumn_6.setText("几天后回访");
		dataTableColumn_6.setWidth(100);

		DataTableColumn dataTableColumn_7 = new DataTableColumn(dataTable,
				SWT.NONE, "hlzq");
		dataTableColumn_7.setText("几天护理一次");
		dataTableColumn_7.setWidth(100);

		DataTableColumn dataTableColumn_8 = new DataTableColumn(dataTable,
				SWT.NONE, "bz");
		dataTableColumn_8.setText("备注");
		dataTableColumn_8.setWidth(80);
		setParams();
		createGUIAfter();
		this.content = content;
		return content;
	}

	protected void doRemove() {
		if (fwxmSet.getSelectedIndex() != -1) {
			if (MessageDialog.openQuestion(content.getShell(), "操作提示", "确认删除 "
					+ fwxmSet.getValue("xmmc", String.class) + "?")) {
				setParams();
				delFwxm.execute();
				doSearch();// 刷新数据
			}
		} else {
			MessageDialog.openInformation(content.getShell(), "操作提示",
					"请选择需要删除的记录");
		}
	}

	protected void doAdd() {
		FfxmEdit xmbj = new FfxmEdit(FfxmEdit.Add, null);
		FunctionDialog dlg = new FunctionDialog(content.getShell(), xmbj);
		if (dlg.open() == SWT.OK)
			doSearch();
	}

	protected void doEdit() {
		FfxmEdit xmbj = new FfxmEdit(FfxmEdit.Edit, fwxmSet);
		FunctionDialog dlg = new FunctionDialog(content.getShell(), xmbj);
		if (dlg.open() == SWT.OK)
			doSearch();
	}

	protected void doSearch() {
		setParams();
		fwxmSet.update();
	}

	protected void setParams() {
		fwxmSet.setParam(0, dataCombo.getText());
		fwxmSet.setParam(1, dataCombo.getText());
		fwxmSet.setParam(2, text.getText());
		fwxmSet.setParam(3, text.getText());
		fwxmSet.setParam(4, text.getText());
		fwxmSet.setParam(5, btnCheckButton.getSelection());
		delFwxm.setParam(0, 0, fwxmSet.getValue("xmbh"));
	}

	protected void createGUIAfter() {
		lbSet.update();
		fwxmSet.update();
	}
	public static void main(String[] args) {
		FfxmView cwsz = new FfxmView();
		FunctionDialog dlg = new FunctionDialog(new Shell(), cwsz);
		dlg.open();
	}
}
